Building a FreeBSD kernel for debugging
You may want to actually debug the kernel or a kernel module, however, debugging is not enabled by default.
Prereqs
Before getting started it is assumed you have a FreeBSD Install and you have downloaded/installed FreeBSD Source.
The steps are identical to the steps contained in the How to build and install a custom kernel on FreeBSD? article, with the exception of the kernel configuration file.
Step 1 – Create a new kernel config
- Determine your architecture by running this command:
# uname -a FreeBSD 9.0-RELEASE FreeBSD 9.0-RELEASE #0: Tue Jan 3 07:46:30 UTC 2012 root@farrell.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC amd64
- Look at the last text item in the output string. I have amd64 so that is my architecture: amd64
- Change to the directory of the kernel configuration files for your architecture. Remember if you are on a different architecture to replace amd64 in the following command lien with your architecture.
# cd /usr/src/sys/amd64/conf
- Copy GENERIC to a new file.
# cp GENERIC KRNLDBG
- Edit KRNLDBG with your favorite text editor.
- First, change the ident value near the top from GENERIC to KRNLDBG.
ident KRNLDBG
- Add debugging settings to your KRNLDBG configuraiton file.
options KDB options KDB_TRACE options DDB options GDB
- Save and close your new KRNLDBG configuration file.
For the remaining steps, follow the How to build and install a custom kernel on FreeBSD? article, only replace any references to KRNL1 kernel config file with the KRNLDBG config file created in the previous step.
Once you are done building and installing the kernel, you should have debugging enabled.
I happen to be working with a kernel module that is crashing and when it crashes, it automatically places me in a debugging session with the following prompt.
db>
Hi Rhyous,
I visited your site through Google and read many articles, which gave me too much guideline. I am using FreeBSD since version 5.6. But i couldn't pay attension seriously.
Your site give me a new passion.
Thanks for creating a good site.
[…] KGDB with VirtualBox: Debug a Live Kernel (Linux) FreeBSD 10; new X.org, KMS, PKGNG, poudriere… Building a FreeBSD kernel for debugging How to build and install a custom kernel on […]